package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.EventInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.EventInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.EventInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.EventInfoBean;
import cc.rengu.igas.bsps.facade.request.EventInfoQueryRequest;
import cc.rengu.igas.bsps.facade.response.EventInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 工单信息查询
 * Created by 王成 on 2018/10/8.
 */
public class EventInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        EventInfoQueryRequest eventInfoQueryRequest = new EventInfoQueryRequest();
        ConvertUtil.convertOutput(eventInfoQueryRequest);
        return eventInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        EventInfoQueryRequest eventInfoQueryRequest = (EventInfoQueryRequest) request;
        EventInfoQueryResponse eventInfoQueryResponse = new EventInfoQueryResponse();
        /* 参数合法性校验 */
        if (StringUtil.isEmptyOrNull(eventInfoQueryRequest.getUserId()) || StringUtil.isEmptyOrNull(eventInfoQueryRequest.getStartDate()) || StringUtil.isEmptyOrNull(eventInfoQueryRequest.getEndDate())) {
            rglog.error("用户编号<{}>,开始日期<{}>,截止日期<{}>不能为空!", eventInfoQueryRequest.getUserId(), eventInfoQueryRequest.getStartDate(), eventInfoQueryRequest.getEndDate());
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(eventInfoQueryRequest.getPageIndex());
        pageInfo.setPageSize(eventInfoQueryRequest.getPageSize());


        /* 查询所有事件申报信息 */
        EventInfoMapper eventInfoMapper = new EventInfoMapperImpl();
        EventInfo eventInfo = new EventInfo();

        eventInfo.setInstId(eventInfoQueryRequest.getHeader().getInstId());
        eventInfo.setDeclareDate(eventInfoQueryRequest.getStartDate());
        eventInfo.setRemark1(eventInfoQueryRequest.getEndDate());
        eventInfo.setEventType(eventInfoQueryRequest.getEventDeclareType());
        eventInfo.setMchntNo(eventInfoQueryRequest.getMchntNo());
        PageResult pageResult = eventInfoMapper.selectEventInfoByDeclareDatePage(eventInfo, pageInfo);
        List<EventInfoBean> eventInfoList = new ArrayList<>();
        if (null != pageResult) {
            for (Object object : pageResult.getResult()) {
                EventInfoBean eventInfoBean = new EventInfoBean();
                EventInfo eventInfob = (EventInfo) object;
                BeanUtil.beanCopy(eventInfob, eventInfoBean);
                eventInfoBean.setEventDeclareType(eventInfob.getEventType());
                eventInfoBean.setLastOprTime(eventInfob.getLastProcTime());
                eventInfoBean.setEventDeclareInfo(eventInfob.getRemark3());
                eventInfoBean.setDeclareId(eventInfob.getDeclareUser());
                /* 凭证影像信息 */
                List<String> credentialImageInfo = new ArrayList<>();
                if (eventInfob.getVoucherImagePath1() != null) {
                    credentialImageInfo.add(eventInfob.getVoucherImagePath1());
                }
                if (eventInfob.getVoucherImagePath2() != null) {
                    credentialImageInfo.add(eventInfob.getVoucherImagePath2());
                }
                if (eventInfob.getVoucherImagePath3() != null) {
                    credentialImageInfo.add(eventInfob.getVoucherImagePath3());
                }

                eventInfoBean.setCredentialImageInfo(credentialImageInfo);
                eventInfoList.add(eventInfoBean);
            }
            eventInfoQueryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
            eventInfoQueryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());
            eventInfoQueryResponse.setPageIndex(pageResult.getPageInfo().getPageIndex());
            eventInfoQueryResponse.setPageSize(pageResult.getPageInfo().getPageSize());
        }
        eventInfoQueryResponse.setEventInfoList(eventInfoList);
        /* 组装应答报文 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(eventInfoQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        EventInfoQueryResponse eventInfoQueryResponse = (EventInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(eventInfoQueryResponse);
    }

    /**
     * 工单信息转换
     *
     * @param eventInfo         事件信息表流水
     * @param eventInfoBeanList 工单信息
     */
    private void convEventInfo(EventInfo eventInfo, List<EventInfoBean> eventInfoBeanList) {
        EventInfoBean eventInfoBean = new EventInfoBean();
        /* 机构标识 */
        eventInfoBean.setInstId(eventInfo.getInstId());
        /* 事件编号 */
        eventInfoBean.setEventId(eventInfo.getEventId());
        /* 事件申报类型 */
        eventInfoBean.setEventDeclareType(eventInfo.getEventType());
        /* 事件申报内容 */
        eventInfoBean.setEventDeclareInfo(eventInfo.getEventDesc());
        /* 申报人 */
        eventInfoBean.setDeclareId(eventInfo.getDeclareUser());
        /* 申报日期 */
        eventInfoBean.setDeclareDate(eventInfo.getDeclareDate());
        /* 申报时间 */
        eventInfoBean.setDeclareTime(eventInfo.getDeclareTime());
        /* 当前处理状态 */
        eventInfoBean.setEventStatus(eventInfo.getEventStatus());
        /* 处理结果描述 */
        eventInfoBean.setEventProcResult(eventInfo.getEventProcResult());
        /* 最后处理人 */
        eventInfoBean.setCurrentOprId(eventInfo.getCurrentProcUser());
        /* 最后处理时间 */
        eventInfoBean.setLastOprTime(eventInfo.getLastProcTime());
        /* 事件优先级 */
        eventInfoBean.setEventPriority(eventInfo.getEventPriority());
        /* 标题 */
        eventInfoBean.setEventTitle(eventInfo.getEventTitle());
        /* 凭证影像信息 */
        List<String> credentialImageInfo = new ArrayList<>();
        if (eventInfo.getVoucherImagePath1() != null) {
            credentialImageInfo.add(eventInfo.getVoucherImagePath1());
        }
        if (eventInfo.getVoucherImagePath2() != null) {
            credentialImageInfo.add(eventInfo.getVoucherImagePath2());
        }
        if (eventInfo.getVoucherImagePath3() != null) {
            credentialImageInfo.add(eventInfo.getVoucherImagePath3());
        }
        eventInfoBean.setCredentialImageInfo(credentialImageInfo);
        eventInfoBeanList.add(eventInfoBean);
    }
}
